From 51a1dcaa6ed5713520dca36fc58cd9240c08b7ca Mon Sep 17 00:00:00 2001
From: Even Rouault <even.rouault@spatialys.com>
Date: Thu, 7 Sep 2017 17:52:59 +0200
Subject: [PATCH] Avoid malloc poisoning issue when including <pthread.h> of
 uclibc (#1013)

Signed-off-by: Olivier Schonken <olivier.schonken@gmail.com>
---
 src/lib/openjp2/thread.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/src/lib/openjp2/thread.c b/src/lib/openjp2/thread.c
index 8b56aa4d5..af33c2c80 100644
--- a/src/lib/openjp2/thread.c
+++ b/src/lib/openjp2/thread.c
@@ -29,9 +29,6 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include "opj_includes.h"
-
-#include "thread.h"
 #include <assert.h>
 
 #ifdef MUTEX_win32
@@ -46,6 +43,8 @@
 #include <windows.h>
 #include <process.h>
 
+#include "opj_includes.h"
+
 OPJ_BOOL OPJ_CALLCONV opj_has_thread_support(void)
 {
     return OPJ_TRUE;
@@ -289,6 +288,10 @@ void opj_thread_join(opj_thread_t* thread)
 #include <stdlib.h>
 #include <unistd.h>
 
+/* Moved after all system includes, and in particular pthread.h, so as to */
+/* avoid poisoning issuing with malloc() use in pthread.h with ulibc (#1013) */
+#include "opj_includes.h"
+
 OPJ_BOOL OPJ_CALLCONV opj_has_thread_support(void)
 {
     return OPJ_TRUE;
@@ -425,6 +428,8 @@ void opj_thread_join(opj_thread_t* thread)
 #else
 /* Stub implementation */
 
+#include "opj_includes.h"
+
 OPJ_BOOL OPJ_CALLCONV opj_has_thread_support(void)
 {
     return OPJ_FALSE;
